﻿using Pb;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class MahjongHall : Game {

    public GameObject Root;
    EginUser user = EginUser.Instance;
    EginMahjong mahjong = EginMahjong.Instance;
    
    public UILabel UserName;
    public UILabel UID;
    public UILabel CoinCount;
    public UISprite avatarSprit;
    public GameObject ReturnBtn;
    private void Awake()
    {
        UIEventListener.Get(ReturnBtn).onClick = ButtonClick;
        base.StartGameSocket();
        SocketManager.Instance.socketListener = this;
    }
    private void Start()
    {
        UserName.text = user.nickname;
        UID.text = user.Uid;
        string str = user.ReturnRelCoin(int.Parse(user.coinCount));
        CoinCount.text = str;
        avatarSprit.spriteName = user.avatarId.ToString();
        if (user.ControlScene == 1)
        {
            GameObject FriendRoomPanel = NGUITools.AddChild(Root, (GameObject)(Resources.Load(GameMahjongPrafabPath.FriendRoomPanel)));
        }
        else
        {
            GameObject CoinRoom = NGUITools.AddChild(Root, (GameObject)(Resources.Load(GameMahjongPrafabPath.MahjongCoinRoom)));
        }
    }
    void ButtonClick(GameObject button)
    {
        if (button.name == "ReturnBtn")
        {
            Application.LoadLevel(ScenePath.Hall);
        }

    }
    /// <summary>
    /// 监听服务器消息
    /// </summary>
    /// <param name="data"></param>
    public override void SocketReceiveMessage(byte[] data)
    {
        base.SocketReceiveMessage(data);
        byte[] dataLength = data.Skip(0).Take(2).ToArray();
        byte[] dataId = data.Skip(2).Take(4).ToArray();
        byte[] dataConcent = data.Skip(data.Length - (data.Length - 4)).ToArray();
        int TempMsgID = ProtoDataToMsg.Byte2ushort(dataId);
        //牛牛创建房间返回
        if (TempMsgID == MsgId.MahjongOpenRoomResponse)
        {
            Pb.MahjongOpenRoomResponse newpb = Pb.MahjongOpenRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                mahjong.CteatrUid = newpb.RoomInfo.CreaterUid;
                mahjong.roomID = newpb.RoomInfo.RoomId;
                mahjong.totalRound = newpb.RoomInfo.TotalRound;
                mahjong.seatID = newpb.SeatId;
                mahjong.roomStatus = newpb.RoomInfo.RoomStatus;
                Debug.Log(mahjong.seatID + "hall");
                //user.gameUserInfoList.Add(newpb.UserInfo);

                mahjong.FirstLoadUser = true;

                //user.CreatRoomTypeID = (int)newpb.RoomInfo.RoomTypeId;

                mahjong.baseScore = (int)newpb.RoomInfo.BaseScore;
                

                //存储房主到other信息里面去
                int tempSeatID;
                tempSeatID = newpb.SeatId;
                mahjong.Otheruid.Add(tempSeatID, newpb.RoomInfo.CreaterUid);
                mahjong.OtherSeatId.Add(tempSeatID, newpb.SeatId);
                mahjong.OtherSeatStatus.Add(tempSeatID, SEAT_STATUS.Sit);
                mahjong.OtherSex.Add(tempSeatID, mahjong.sex);
                mahjong.OtherIP.Add(tempSeatID, mahjong.IP);
                mahjong.OtherCoinCount.Add(tempSeatID, mahjong.coinCount);
                mahjong.OtherNickName.Add(tempSeatID, mahjong.nickname);
                mahjong.OtherAvatarid.Add(tempSeatID, mahjong.Avatar_id);
                mahjong.OtherScore.Add(tempSeatID, mahjong.Score);
                GameUserInfo userInfo = new Pb.GameUserInfo();
                userInfo.Uid = newpb.RoomInfo.CreaterUid;
                userInfo.SeatId = newpb.SeatId;
                userInfo.SeatStatus = SEAT_STATUS.Sit;
                userInfo.Sex = mahjong.sex;
                userInfo.Ip = mahjong.IP;
                userInfo.BagCoinCount = mahjong.coinCount;
                userInfo.Score = mahjong.Score;
                userInfo.AvatarId = mahjong.Avatar_id;
                userInfo.Nickname = mahjong.nickname;

                user.gameUserInfoList.Add(userInfo);
                //user.gameUserInfoList.Add(newpb.UserInfo);
                Application.LoadLevelAsync(ScenePath.GameMahjongPlay);
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }
        }
        //mj根据ID加入房间返回
        else if (TempMsgID == MsgId.MahjongEnterRoomResponse)
        {
            Pb.MahjongEnterRoomResponse newpb = Pb.MahjongEnterRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                mahjong.CteatrUid = newpb.RoomInfo.CreaterUid;
                mahjong.roomID = newpb.RoomInfo.RoomId;
                mahjong.totalRound = newpb.RoomInfo.TotalRound;
                mahjong.seatID = newpb.SeatId;
                mahjong.roomStatus = newpb.RoomInfo.RoomStatus;
                //user.gameUserInfoList = newpb.UserInfoList.ToList();
                
                mahjong.baseScore = (int)newpb.RoomInfo.BaseScore;


                //存储其他玩家信息
                mahjong.Otheruid = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Uid);
                mahjong.OtherSeatId = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.SeatId);
                mahjong.OtherSeatStatus = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.SeatStatus);
                mahjong.OtherSex = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Sex);
                mahjong.OtherIP = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Ip);
                mahjong.OtherCoinCount = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.BagCoinCount);
                mahjong.OtherScore = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Score);
                mahjong.OtherNickName = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Nickname);
                mahjong.OtherAvatarid = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.AvatarId);
                Debug.Log("存储其他玩家信息");

                user.gameUserInfoList = newpb.UserInfoList.ToList();
                Application.LoadLevelAsync(ScenePath.GameMahjongPlay);
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }
        }
        //mj随机进入房间返回
        else if (TempMsgID == MsgId.MahjongRandomEnterRoomResponse)
        {
            Pb.MahjongRandomEnterRoomResponse newpb = Pb.MahjongRandomEnterRoomResponse.Parser.ParseFrom(dataConcent);
            Debug.Log("消息ID" + TempMsgID + "消息内容" + newpb);
            if (newpb.Status == STATUS.Success)
            {
                mahjong.CteatrUid = newpb.RoomInfo.CreaterUid;
                mahjong.roomID = newpb.RoomInfo.RoomId;
                mahjong.totalRound = newpb.RoomInfo.TotalRound;
                mahjong.seatID = newpb.SeatId;
                mahjong.roomStatus = newpb.RoomInfo.RoomStatus;
                //user.gameUserInfoList = newpb.UserInfoList.ToList();
                mahjong.baseScore = (int)newpb.RoomInfo.BaseScore;

                if (newpb.SeatId == 1)
                {
                    Debug.Log("存储房主到other信息里面去");
                    mahjong.FirstLoadUser = true;
                    //存储房主到other信息里面去
                    int tempSeatID;
                    tempSeatID = newpb.SeatId;
                    mahjong.Otheruid.Add(tempSeatID, newpb.RoomInfo.CreaterUid);
                    mahjong.OtherSeatId.Add(tempSeatID, newpb.SeatId);
                    mahjong.OtherSeatStatus.Add(tempSeatID, SEAT_STATUS.Sit);
                    mahjong.OtherSex.Add(tempSeatID, user.sex);
                    mahjong.OtherIP.Add(tempSeatID, user.IP);
                    mahjong.OtherCoinCount.Add(tempSeatID, mahjong.coinCount);
                    mahjong.OtherNickName.Add(tempSeatID, mahjong.nickname);
                    mahjong.OtherAvatarid.Add(tempSeatID, mahjong.Avatar_id);
                    mahjong.OtherScore.Add(tempSeatID, mahjong.Score);
                }
                //存储其他玩家信息
                mahjong.Otheruid = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Uid);
                mahjong.OtherSeatId = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.SeatId);
                mahjong.OtherSeatStatus = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.SeatStatus);
                mahjong.OtherSex = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Sex);
                mahjong.OtherIP = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Ip);
                mahjong.OtherCoinCount = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.BagCoinCount);
                mahjong.OtherScore = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Score);
                mahjong.OtherNickName = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.Nickname);
                mahjong.OtherAvatarid = newpb.UserInfoList.ToDictionary(t => t.SeatId, t => t.AvatarId);
                mahjong.seatStatus = mahjong.OtherSeatStatus[newpb.SeatId]; //存储玩家的seatstatus
                Debug.Log("存储其他玩家信息");

                user.gameUserInfoList = newpb.UserInfoList.ToList();
                Application.LoadLevelAsync(ScenePath.GameMahjongPlay);
            }
            else
            {
                user.logMsg = newpb.Msg;
                GameObject Set = NGUITools.AddChild(Root, (GameObject)(Resources.Load("LogMsg")));
            }

        }
        //网络异常
        else if (TempMsgID == MsgId.NetWorkAbnormalMessage)
        {
            GameObject NetWorkAbnormal = NGUITools.AddChild(GameObject.Find("UI Root"), (GameObject)(Resources.Load("NetWorkAbnormal")));
        }
    }

}
